package cn.wang.web.sysadmin;

import cn.wang.entity.sysadmin.Dept;
import cn.wang.service.sysadmin.DeptService;

import cn.wang.utils.Pagination;
import cn.wang.web.BaseAction;

import com.opensymphony.xwork2.ModelDriven;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import java.util.List;

/**
 * Created by john on 2017/11/1.
 */
@Controller
public class DeptAction extends BaseAction implements ModelDriven<Dept>{
    private Pagination page =new Pagination();
    private List<Dept> deptList;
    private Dept model=new Dept();

    @Autowired
    private DeptService deptService;

 //分页
    public String list(){
        page =deptService.findPage(page.getPageNo(),page.getPageSize());
        page.setUrl("deptAction_list");
        push(page);
        return "list";
    }
    //查询所有部门
   public String tocreate(){
       deptList =deptService.findAll();
        return"tocreate";
   }
   //查寻单个部门信息
    public String toview(){
        deptService.findById(model.getId());
       return "toview";
    }
   //添加部门
   public String insert(){
       deptService.save(model);
       return "rlist";

   }
   //更新部门 先查询
    public  String toupdate(){

       //查询所有部门
        deptList =deptService.findAll();
        //根据id查询老部门
        model =deptService.findById(model.getId());
        //移除本部门
        deptList.remove(model);
       return "toupdate";
    }
    public String update(){
        deptService.update(model);
        return "rlist";
    }
    //删除部门 多个部门删除时 id 字符类型id会 拼接在一起(,空格)
    public String delete(){
        if(model.getId()==null){
            return "rlist";
        }
       String[] ids=model.getId().split(", ");
        deptService.delete(ids);
        return "rlist";
    }
    public void setPage(Pagination page) {
        this.page = page;
    }

    public List<Dept> getDeptList() {
        return deptList;
    }

    @Override
    public Dept getModel() {
        //将字段名设为model使一行代码两用 1模型驱动 2 字段set方法
        return model;
    }
}
